#include "registerwindow.h"
#include "ui_registerwindow.h"
#include "loginwindow.h"

RegisterWindow::RegisterWindow(LoginWindow* loginwindow,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWindow),
    login(loginwindow)
{
    ui->setupUi(this);
    connect(SocketManager::instance(), &SocketManager::registerResponse,
            this, &RegisterWindow::handleRegisterResponse);

    //connect(getsocket(),&QTcpSocket::readyRead,this,&RegisterWindow::handleTORead);
}

RegisterWindow::~RegisterWindow()
{
    delete ui;
}

void RegisterWindow::on_pushButton_clicked()
{
    QString username = ui->username->text();
    QString password = ui->password->text();

    QJsonObject json;
    json["MessageType"] = static_cast<int>(Protocol::MessageType::Msg_Registe);
    json["username"] = username;
    json["password"] = password;
    QJsonDocument doc(json);

    QByteArray array =  doc.toJson(QJsonDocument::Compact);
    array.append('\n');
    SocketManager::instance()->getsocket()->write(array);
}

void RegisterWindow::handleRegisterResponse(QJsonObject obj)
{
    bool issuccess = obj["success"].toBool();
    QString message = obj["message"].toString();

    if (issuccess)
    {
        QMessageBox::information(this, "提示", "注册成功");
        this->close();
        login->show();
    }
    else
    {
        ui->info->setStyleSheet("color:red");
        ui->info->setText("注册失败：" + message);
    }
}

//void RegisterWindow::handleTORead()
//{
//    QJsonParseError error;
//    if(getsocket()->canReadLine())
//    {
//        QByteArray data = getsocket()->readLine().trimmed();

//        QJsonDocument doc = QJsonDocument::fromJson(data,&error);

//        if(error.error!=QJsonParseError::NoError)
//        {
//            ui->info->setStyleSheet("color:red");
//            ui->info->setText("Json串解析失败 "+error.errorString());
//            return ;
//        }

//        if(!doc.isObject())
//        {
//            ui->info->setStyleSheet("color:red");
//            ui->info->setText("服务器返回的不是Json对象");
//            return ;
//        }

//        QJsonObject retjson = doc.object();
//        //解析json串内容
//        int type = retjson["MessageType"].toInt();
//        qDebug()<<"type"<<type;
//        bool issuccess = retjson["success"].toBool();
////        QString username = retjson["username"].toString();
//        QString message = retjson["message"].toString();

//        if(type == static_cast<int>(Protocol::ResponseType::Res_Registe))
//        {
//            if(issuccess)
//            {
//                //注册成功弹窗
//                QMessageBox::information(this,"提示","注册成功");
//                //转到登录界面
//                this->close();
//                login->show();
//            }
//            else
//            {
//                ui->info->setStyleSheet("color:red");
//                ui->info->setText("注册失败" + message);
//                return ;
//            }
//        }
//    }
//    qDebug()<<"未执行if";
//    //QByteArray data = getsocket()->readAll();


//}

//QTcpSocket *RegisterWindow::getsocket()
//{
//    return SocketManager::getTcpsocket();
//}

void RegisterWindow::on_pushButton_2_clicked()
{
    this->close();
    login->show();
}
